home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / TCPGATE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-24  |  4.3 KB  |  197 lines

  1. #include "global.h"
  2. #ifdef TCPGATE
  3. #include "commands.h"
  4. #include "mbuf.h"
  5. #include "netuser.h"
  6. #include "bldsaddr.h"
  7. #include "stats.h"
  8. static void tcpgateserv(int s,void *ds,void *p);
  9. static void gate_output(int s1,void *s2,void *p);
  10. extern unsigned short getnextport (void);
  11.  
  12.  
  13. #define Gatetdisc (60*10)    /* default to 10 minutes timeout */
  14.  
  15.  
  16. #if !defined(_lint)
  17. static char rcsid[] OPTIONAL = "$Id: tcpgate.c,v 1.18 1997/05/24 12:45:59 root Exp root $";
  18. #endif
  19.  
  20.  
  21. struct dest {
  22.   int port;
  23.   int socket;
  24.   char *name;
  25.   struct dest *next;
  26. };
  27.  
  28. static struct dest *dests;
  29.  
  30.  
  31. int
  32. tcpgate0 (int argc OPTIONAL, char *argv[], void *p OPTIONAL)
  33. {
  34. struct dest *dp, *dp2;
  35. int port;
  36.  
  37.     port = atoip(argv[1]);
  38.     dp = dests;
  39.     if (dp->port == port)    {
  40.         dests = dp->next;
  41.     } else    {
  42.         for ( ; dp && dp->next->port != port; dp = dp->next) ;
  43.         if (!dp || !dp->next)    {
  44.             tprintf ("The tcpgate server is not active on port %d - nothing to stop!\n", port);
  45.             return 0;
  46.         }
  47.         dp2 = dp->next;
  48.         dp->next = dp2->next;
  49.         dp = dp2;
  50.     }
  51.  
  52.     close_s (dp->socket);
  53.     if (dp->name)
  54.         free (dp->name);
  55.     free(dp);
  56.     tprintf ("The tcpgate server removed from port %d\n", port);
  57.     return 0;
  58. }
  59.  
  60.  
  61.  
  62. int tcpgate1(int argc, char *argv[], void *p OPTIONAL)
  63. {
  64. char buf[80];
  65. int port;
  66. char *name;
  67. struct dest *dp;
  68.  
  69.     port = atoip(argv[1]);
  70. #if 0
  71.     if (argc < 3)
  72.         sprintf(name = buf, "loopback:%d", port);
  73.     else
  74. #endif
  75.         name = argv[2];
  76.     for (dp = dests; dp && dp->port != port; dp = dp->next) ;
  77.     if (!dp) {
  78.         dp = (struct dest *) mallocw(sizeof(struct dest));
  79.         dp->port = port;
  80.         dp->name = 0;
  81.         dp->next = dests;
  82.         dests = dp;
  83.     }
  84.     if (dp->name) free(dp->name);
  85.     dp->name = strdup(name);
  86.     dp->socket = -1;
  87.  
  88.     sprintf (buf, "TCPGATE %d listener", port);
  89.     return (installserver (argc, argv, &dp->socket, buf, port,
  90.         INADDR_ANY, "TCPGATE server", tcpgateserv, 512, dp));
  91. }
  92.  
  93.  
  94.  
  95. static void
  96. tcpgateserv (int s, void *ds, void *p OPTIONAL)
  97. {
  98. struct dest *dp = (struct dest *)ds;
  99. struct mbuf *bp;
  100. int addrlen;
  101. struct sockaddr *addr = 0;
  102. int rmtsock;
  103. struct proc *rxproc;
  104. struct sockaddr_in lsocket;
  105. #if 0
  106. register struct usock *up;
  107. #endif
  108. int datalen;
  109.  
  110.     (void) sockowner(s,Curproc);    /* We own it now */
  111.     /* Secede from the parent's sockets, and use the network socket that
  112.      * was passed to us for both input and output. The reference
  113.      * count on this socket will still be 1; this allows the domboxbye()
  114.      * command to work by closing that socket with a single call.
  115.      * If we return, the socket will be closed automatically.
  116.      */
  117.     close_s(Curproc->output);
  118.     close_s(Curproc->input);
  119.     Curproc->output = Curproc->input = s;
  120.  
  121.     log(s, "open TCPGATE server on port %d", dp->port);
  122.     lsocket.sin_family = AF_INET;
  123.     lsocket.sin_addr.s_addr = INADDR_ANY;
  124.     lsocket.sin_port = getnextport();
  125.  
  126.     addr = build_sockaddr(dp->name, &addrlen);
  127.     if (!addr || (rmtsock = socket(addr->sa_family, SOCK_STREAM, 0)) <= 0 ||
  128.         bind(rmtsock,(char *)&lsocket,sizeof(lsocket)) < 0 ||
  129.         connect(rmtsock, (char *)addr, addrlen))
  130.             return;
  131.  
  132. #ifdef STATS_USE
  133.     STATS_adduse (1);
  134.     MiscUsers++;
  135. #endif
  136.     /* Fork off the transmit process */
  137.     rxproc = newproc("tcpgate_out",1024,gate_output,s,(void *)rmtsock,&rxproc,0);
  138.  
  139.     /* Process input on the connection */
  140.     while (1)    {        /*lint !e716 !e774 */
  141.         if((datalen = socklen(s,0)) > 0)    {
  142.             if (recv_mbuf(s,&bp,0,NULLCHAR,0) == -1)
  143.                 break;
  144.             if (send_mbuf(rmtsock,bp,0,NULLCHAR,0) == -1)
  145.                 break;
  146.         } else if(datalen < 0)
  147.             break;
  148. /*        if((up = itop(s)) == NULLUSOCK)
  149.             break;
  150.         if(up->cb.p == NULLCHAR)
  151.             break;
  152.         if (up->cb.local->peer->cb.local->peer == NULLUSOCK)
  153.             break;
  154.         if (up->cb.tcb->user == -1)
  155.             break;
  156.         if (up->cb.local->flags == LOC_SHUTDOWN)
  157.             break;        */
  158.         if (rxproc == NULLPROC)
  159.             break;
  160.         if (!datalen)
  161.             kpause (200);
  162.     }
  163.     killproc(rxproc);
  164. #ifdef STATS_USE
  165.     MiscUsers--;
  166. #endif
  167.     log(s, "closed TCPGATE server on port %d", dp->port);
  168.     close_s(s);
  169.     close_s(rmtsock);
  170.     s = -1;
  171. }
  172.  
  173.  
  174.  
  175. static void
  176. gate_output (int s1, void *s2, void *rx)
  177. {
  178. struct mbuf *bp;
  179. int sock2;
  180. struct proc **rxproc;
  181.  
  182.     sock2 = (int)s2;
  183.     rxproc = (struct proc **)rx;
  184.  
  185.     for ( ; ; )    {
  186.         kalarm (Gatetdisc * 1000L);
  187.         if (recv_mbuf(sock2,&bp,0,NULLCHAR,0) == -1)
  188.             break;
  189.         kalarm (0);
  190.         if(send_mbuf(s1,bp,0,NULLCHAR,0) == -1)
  191.             break;
  192.     }
  193.     *rxproc = NULLPROC;
  194. }
  195.  
  196. #endif /* TCPGATE */
  197.